package com.hxzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.dto.PageDTO;
import com.hxzy.entity.TbNewbeeMallAdminUser;
import com.hxzy.mapper.TbNewbeeMallAdminUserMapper;
import com.hxzy.service.TbNewbeeMallAdminUserService;
import com.hxzy.util.BCrypt;
import com.hxzy.vo.BootstrapTableVO;
import com.hxzy.vo.ResultEnum;
import com.hxzy.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//开启事务自动提交
@Transactional
@Service
public class TbNewbeeMallAdminUserImpl implements TbNewbeeMallAdminUserService {
    @Autowired
    private TbNewbeeMallAdminUserMapper tbNewbeeMallAdminUserMapper;


    @Override
    public TbNewbeeMallAdminUser login(String loginUserName, String loginPassword) {
        return  this.tbNewbeeMallAdminUserMapper.login(loginUserName);
    }

        @Override
    public BootstrapTableVO search(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNumber(),pageDTO.getPageSize());

        List<TbNewbeeMallAdminUser> search = this.tbNewbeeMallAdminUserMapper.search(pageDTO);
        BootstrapTableVO tableVO=new BootstrapTableVO(new PageInfo(search));
        return tableVO;
    }

    @Override
    public int insert(TbNewbeeMallAdminUser record) {
        return this.tbNewbeeMallAdminUserMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKeySelective(TbNewbeeMallAdminUser record) {
        return this.tbNewbeeMallAdminUserMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ResultVO addOrUpdate(TbNewbeeMallAdminUser tbNewbeeMallAdminUser) {
        ResultVO resultVO=null;
        int count=0;
        System.out.println(this.tbNewbeeMallAdminUserMapper.selectByName(tbNewbeeMallAdminUser.getLoginUserName()));
        if (this.tbNewbeeMallAdminUserMapper.selectByName(tbNewbeeMallAdminUser.getLoginUserName())==null){
            tbNewbeeMallAdminUser.setLoginPassword(BCrypt.hashpw(tbNewbeeMallAdminUser.getLoginPassword(),BCrypt.gensalt()));
          count=  this.tbNewbeeMallAdminUserMapper.insert(tbNewbeeMallAdminUser);
        }else {
            count=this.tbNewbeeMallAdminUserMapper.updateByPrimaryKeySelective(tbNewbeeMallAdminUser);
        }
        if (count>0){
            resultVO = ResultVO.success(ResultEnum.SUCDESS.getMessage());
        }else{
            resultVO=ResultVO.failed(ResultEnum.PARAMS_FAILED);
        }

        return resultVO;
    }

    @Override
    public TbNewbeeMallAdminUser selectByName(String userName) {
        return this.tbNewbeeMallAdminUserMapper.selectByName(userName);
    }


}
